﻿using Microsoft.Practices.Prism.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using WebServerConnectorDll;

namespace FridgeManagerApp
{
    [DataContract]
    public class WDomino : NotificationObject
    {
        private int dominoUID;
        private int dominoTypeUID;
        private float dominoStockPrice;
        private float dominoSellPrice;
        private int isSold;

        [DataMember(Order = 0)]
        public int DominoUID
        {
            get
            {
                return dominoUID;
            }
            set
            {
                dominoUID = value;
                RaisePropertyChanged("DominoUID");
            }
        }

        [DataMember(Order = 1)]
        public int DominoTypeUID
        {
            get
            {
                return dominoTypeUID;
            }
            set
            {
                dominoTypeUID = value;
                RaisePropertyChanged("DominoTypeUID");
            }
        }

        [DataMember(Order = 2)]
        public float DominoStockPrice
        {
            get
            {
                return dominoStockPrice;
            }
            set
            {
                dominoStockPrice = value;
                RaisePropertyChanged("DominoStockPrice");
            }
        }

        [DataMember(Order = 3)]
        public float DominoSellPrice
        {
            get
            {
                return dominoSellPrice;
            }
            set
            {
                dominoSellPrice = value;
                RaisePropertyChanged("DominoSellPrice");
            }
        }

        [DataMember(Order = 4)]
        public int IsSold
        {
            get
            {
                return isSold;
            }
            set
            {
                isSold = value;
                RaisePropertyChanged("IsSold");
            }
        }

        public WDomino()
        {
            DominoUID = -1;
            DominoTypeUID = -1;
            DominoStockPrice = -1;
            DominoSellPrice = -1;
            IsSold = -1;
        }

        public WDomino(LDomino p)
        {
            DominoUID = p.DominoUID;
            DominoStockPrice = p.DominoStockPrice;
            DominoSellPrice = p.DominoSellPrice;
            IsSold = p.IsSold;
            DominoTypeUID = WDominoType.GetDominoTypeUID(p.DominoTypeSelected);
        }

        public static List<WDomino> GetAll()
        {
            HttpResponse response = FridgeConnector.PostMessage(EStudy.Domino, EMethod.SelectAll, "");
            if (response.Status == "Success")
            {
                return JSON.parse<List<WDomino>>(response.Data1);
            }

            return null;
        }

        public static HttpResponse Insert(List<WDomino> pList)
        {
            String postData = JSON.stringify(pList);
            return FridgeConnector.PostMessage(EStudy.Domino, EMethod.Insert, postData);
        }

        public static HttpResponse Update(List<WDomino> pList)
        {
            String postData = JSON.stringify(pList);
            return FridgeConnector.PostMessage(EStudy.Domino, EMethod.Update, postData);
        }

        public static HttpResponse Delete(List<WDomino> pList)
        {
            String postData = JSON.stringify(pList);
            return FridgeConnector.PostMessage(EStudy.Domino, EMethod.Delete, postData);
        }
    }
}
